﻿/**********************************天使物流*******************************************
* 功能描述:系统设置——操作员管理
* 作者:孟帅龙
* 时间:2022/1/11
* 
**********************************天使物流*******************************************/
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.System.Operators;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.System.Operators
{
    public class OperatorRepository :DapperDbContextRepository,IOperatorRepository
    {
        public OperatorRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 操作员管理——查询分页显示
        /// <summary>
        /// 操作员管理——查询分页显示
        /// </summary>
        /// <param name="OperatorNumber"></param>
        /// <param name="OperatorName"></param>
        /// <param name="DeptId"></param>
        /// <param name="OperatorPhone"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<OperatorDto>> GetOperatorPageShow(string OperatorNumber, string OperatorName, int? DeptId, string OperatorPhone, int pageindex, int pagesize)
        {
            PageResponse<OperatorDto> response = new PageResponse<OperatorDto>();
            var sql = "select a.*,b.DeptName,d.CompanyName,e.RoleName from Operator a join Department b on a.DeptId =b.Id join ORRelation c on a.OperatorNumber=c.OperatorId join Company d on a.CpmpanyId=d.Id join Role e on c.RoleId=e.RoleNumber where a.IsDelete=0";
            if (!string.IsNullOrWhiteSpace(OperatorNumber))
            {
                sql += $" and a.OperatorNumber like '%" + OperatorNumber + "%'";
            }
            if (!string.IsNullOrWhiteSpace(OperatorName))
            {
                sql += $" and a.OperatorName like '%" + OperatorName + "%'";
            }
            if (DeptId > 0)
            {
                sql += $" and a.DeptId='{DeptId}'";
            }
            if (!string.IsNullOrWhiteSpace(OperatorPhone))
            {
                sql += $" and a.OperatorPhone like '%" + OperatorPhone + "%'";
            }
            var list = await this.QueryAsync<OperatorDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $"order by a.Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<OperatorDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        #endregion

        #region 操作员管理——部门显示
        /// <summary>
        /// 操作员管理——部门显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<DeptDto>> GetDepartment()
        {
            PageResponse<DeptDto> response = new PageResponse<DeptDto>();
            var sql = $"select * from Department where IsDelete=0  ";
            response.PageData = await this.QueryAsync<DeptDto>(sql, null);
            return response.PageData;
        }
        #endregion

        #region 操作员管理——逻辑删除
        /// <summary>
        /// 操作员管理——逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<PageResponse<OperatorDto>> DeleteOperator(int id)
        {
            PageResponse<OperatorDto> response = new PageResponse<OperatorDto>();
            var sql = $"update Operator set IsDelete=1 where Id={id}";
            response.Code = await ExecuteAsync(sql, null);
            return response;
        }
        #endregion

        #region 操作员管理——角色显示
        /// <summary>
        /// 操作员管理——角色显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoleDto>> GetRole()
        {
            PageResponse<RoleDto> response = new PageResponse<RoleDto>();
            var sql = $"select * from Role where IsDelete=0  ";
            response.PageData = await this.QueryAsync<RoleDto>(sql, null);
            return response.PageData;
        }
        #endregion

        #region 操作员管理——编辑
        /// <summary>
        /// 操作员管理——编辑
        /// </summary>
        /// <param name="operatorDto"></param>
        /// <returns></returns>
        public async Task<PageResponse<OperatorDto>> PutUpdateOperator(OperatorDto operatorDto)
        {
            PageResponse<OperatorDto> response = new PageResponse<OperatorDto>();
            var sql = $"update Operator set OperatorNumber='{operatorDto.OperatorNumber}',OperatorName='{operatorDto.OperatorName}',CpmpanyId='{operatorDto.CpmpanyId}',OperatorPhone='{operatorDto.OperatorPhone}',CreateTime='{operatorDto.CreateTime}',OperatorState='{operatorDto.OperatorState}',Account='{operatorDto.Account}',Password='{operatorDto.Password}',IsDelete='{operatorDto.IsDelete}',AffirmPassword='{operatorDto.AffirmPassword}',DeptId='{operatorDto.DeptId}' where Id='{operatorDto.Id}'";
            response.Code = await ExecuteAsync(sql, null);
            return response;
        }
        #endregion

        #region 操作员管理——批量删除
        /// <summary>
        /// 操作员管理——批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<PageResponse<OperatorDto>> DeleteBatchOperator(string ids)
        {
            PageResponse<OperatorDto> response = new PageResponse<OperatorDto>();
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Operator set IsDelete=1 where Id={item}";
                response.Code = await ExecuteAsync(sql, null);
            }
            return response;
        }
        #endregion

        #region 操作员管理——添加
        /// <summary>
        /// 操作员管理——添加
        /// </summary>
        /// <param name="operatorDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddOperator(OperatorDto operatorDto)
        {
            var worker = new IdWorker(1, 1);
            var id = worker.NextId().ToString();
            var sql = $"insert into Operator values('{id}','{operatorDto.OperatorName}','{operatorDto.CpmpanyId}','{operatorDto.OperatorPhone}',getdate(),'0','{operatorDto.Account}','{operatorDto.Password}','0','{operatorDto.AffirmPassword}','{operatorDto.DeptId}','{operatorDto.RoleId}')";

            return await ExecuteAsync (sql, operatorDto);


        }
        #endregion

    }

}
